#include "Test.Batch.h"

#undef  APP_NAME
#define APP_NAME	TestFont
#undef  APP_BASE
#define APP_BASE	TestBatch


class APP_NAME : public APP_BASE
{
protected:
	FontBatch		fontBatch;
	ext_string		text;

	BatchMaterial	mtr;


public:
	APP_NAME (IPlatform *platform, EDevice::type deviceType) :
		APP_BASE( platform, deviceType )
	{}

	void Update (float dt) override
	{
		TestApp::Update( dt );

		ESS()->GetGraphicsEngine()->GetViewport()->Apply();
		ESS()->GetGLStateManager()->ClearColor( color4f( 0.0f, 0.0f, 0.0f, 1.0f ) );
		//ESS()->GetGLStateManager()->ClearColor( color4f( 0.0f, 0.5f, 0.2f, 1.0f ) );

		rect	text_region = rect(-0.5f, -0.5f, 0.5f, 0.5f );

		batch.AddBatch( material, Rect2D( text_region, rect(), color4u(255, 0, 0, 120) ) );
		fontBatch.Draw( text_region, text );
		
		//mtr.SetTexture( 0, fontBatch.GetFont().ToPtr< VectorFont >()->GetTexture() );
		//batch.AddBatch( mtr, Rect2D( rect(-0.95f, -0.95f, 0.95f, 0.95f), rect(0.0f, 0.0f, 1.0f, 1.0f), color4u(255) ) );

		batch.Flush();
		batch.Clear();
		
		ESS()->GetGraphicsEngine()->SwapBuffers();
	}

	void OnEnter () override
	{
		APP_BASE::OnEnter();

		/*
		BitmapFontPtr font;
		BitmapFont::BitmapFontLoadParams	params;
		params.height	= 64;
		params.mipmaps	= true;

		resMngr.Load( EResource::BITMAP_FONT, "font/gui_font.fnt", font );
		/*/
		VectorFontPtr	font;
		resMngr.Load( EResource::VECTOR_FONT, "font/gui_font.fnt", font );
		//*/
		ProgramPtr	fontProgram = ESS()->GetGraphicsEngine()->GetShaderManager()->
								GetDefaultProgram( EDefaultShader::COLORED_WITH_OPACITY_MAP );
		
		fontBatch.SetFont( font );
		fontBatch.SetProgram( fontProgram );
		
		
		fontBatch.SetColor( color4u(255) );
		fontBatch.SetScale( /*font->GetDefaultScale()*/ 0.06f );
		fontBatch.SetSpacing( 0.2f );
		fontBatch.SetDirection( ETextDirection::DEFAULT );

		text.Parse( "1234567890" );
		//text.Parse( "![]-=+_()@#$%^&*/?><,.\"\\~`0123456789ABCDEFGHIJKLMNOPQSRTVWXYZabcdefghijklmnopqsrtvwxyz" );
		//text.Parse( "Qw\nEr\nTy^\"\n\n09\n87\n12\n34" );
		//text.Parse( "#[scale:2.0]qwerty#[spacing:2.1]\n0987#[color:FF00FF0F]1234" );


		mtr.SetProgram( ESS()->GetGraphicsEngine()->GetShaderManager()->
								GetDefaultProgram( EDefaultShader::COLORED_WITH_OPACITY_MAP ) );
	}

	void OnExit () override
	{
		mtr.Clear();
		fontBatch.Clear();
		APP_BASE::OnExit();
	}
};